package com.shd.day06;

import java.util.stream.LongStream;

/**
 * 并行流操作
 * Created by shanhaodong on 2018/7/30
 */
public class ParallelOperation {
    public static void main(String[] args) {
        long sum = sequentialSum(100L);
        int i = Runtime.getRuntime().availableProcessors();
        System.out.println(i);
    }

    /**
     * 普通流求和
     *
     * @param n
     * @return
     */
    public static long sequentialSum(long n) {
        return LongStream.iterate(1L, i -> i + 1).limit(n).reduce(0L, Long::sum);
    }

    /**
     * 并行流求和
     *
     * @param n
     * @return
     */
    public static long parallelSum(long n) {
        return LongStream.iterate(1L, i -> i + 1).limit(n).parallel().reduce(0L, Long::sum);
    }
}
